<template>
	<div>
		<!-- 弹窗：选择审批人员 -->
    <el-dialog 
      title="选择审批人员"
      append-to-body
      :visible.sync="chooseApproveUserDialog"
      :close-on-click-modal="false" 
      :before-close="closeChooseApproveUserDialog"
      width="60%"
      class="choose_approve_user_dialog"
    >

      <!-- 内容 -->
      <avue-crud 
        :option="auOption"
        :data="auData"
        @selection-change="auSelectionChange"
        ref="auCrud"
      >
      </avue-crud>

      <!-- 按钮 -->
      <div class="choose_approve_user_footer">
        <el-button 
          type="primary"
          size="small"
          @click="confirmChooseApproveUser()">
          确 定
        </el-button>
        <el-button 
          size="small"
          @click="closeChooseApproveUserDialog()">
          取 消
        </el-button>
      </div>

    </el-dialog>
	</div>
</template>

<script>
import { getUserList } from "@/api/basedata/interestedpartymange/relatedparty";

export default {
  props: {
    chooseApproveUserDialog: {default: false},
    selectedUsers: {default: []}
  },
  data() {
    return {
      auData: [],
      auSelectionList: [],
      auOption: {
        calcHeight: 30,
        tip: false,
        index: true,
        selection: true,
        addBtn: false,
        index: false,
        refreshBtn: false,
        columnBtn: false,
        menu: false,
        column: [
          {
            label: "姓名",
            prop: "realName",
            align: "center"
          },
          {
            label: "性别",
            prop: "sexName",
            align: "center"
          },
          {
            label: "部门",
            prop: "deptName",
            align: "center"
          },
          {
            label: "电话",
            prop: "phone",
            align: "center"
          }
        ]
      }
    }
  },
  watch: {
    chooseApproveUserDialog(){
      if(this.chooseApproveUserDialog === true){
        this.initData();
      }
    }
  },
  mounted() {
    this.initData();
  },
  methods: {

    // 初始化评估历史弹窗
    initData() {
      this.auOnload();
    },

    // 获取该公司员工列表
    auOnload() {
      getUserList().then(res => {
        this.auData = res.data.data;
      }).then(() => {
        this.checkSelectedUsers();
      });
    },

    // 勾选已在列表中的人员
    checkSelectedUsers() {
      if(this.selectedUsers.length !== 0) {
        let userIds = [];
        this.selectedUsers.forEach(item => {
          userIds.push(item.id);
        });
        this.$nextTick(() => {
          this.auData.forEach(row => {
            if(userIds.indexOf(row.id) >= 0 && this.$refs.auCrud !== undefined) {
              this.$refs.auCrud.toggleRowSelection(row, true);
            }
          })
        });
      } else {
        if(this.$refs.auCrud !== undefined) {
          this.$refs.auCrud.toggleSelection();
        }
      }
    },

    // 勾选审批人员
    auSelectionChange(list) {
      this.auSelectionList = list;
    },

    // 确定选择审批人员
    confirmChooseApproveUser() {
      if(this.auSelectionList.length === 0) {
        this.$message.warning("请选择至少一个审批人员!");
        return;
      }
      this.$emit('confirmChooseApproveUser', this.auSelectionList);
    },

    // 关闭、取消选择审批人员弹窗
    closeChooseApproveUserDialog() {
      this.$emit('closeChooseApproveUserDialog');
    }

  }
}
</script>

<style lang="scss" scoped>
.choose_approve_user_footer {
  text-align: center;
  margin-top: 15px;

  .el-button {
    width: 100px;
    height: 35px;
  }
}
</style>